<?php

include 'crudAbstract.class.php';
include '../config/database.class.php';

class SaidasController extends CrudAbstract{
    
    private $conexao = NULL;

    function __construct() {

        $this->conexao = Database::conectar();
    }
    
    /**
     * 
     * @param Saidas_model $objvo
     * Método adicionar saida 
     */    
    public function adicionar($objvo) {

        try {

            if ($objvo instanceof Saidas_model) {

                $stmt = $this->conexao->prepare("INSERT INTO tbl_saidas " . 
                        "(codsaida, produto, funcionario, qtdsaida) " .
                        "VALUES(?, ?, ?, ?)");

                $stmt->bindValue(1, $objvo->getCodsaida());
                $stmt->bindValue(2, $objvo->getProduto());
                $stmt->bindValue(3, $objvo->getFuncionario());
                $stmt->bindValue(4, $objvo->getQtdsaida());                
                $stmt->execute();
                
            } else {

                print 'Nao e instancia';
            }
        } catch (PDOException $exception) {

            print 'ERROR:. ' . $exception->getMessage();
        }
    }
    
    /**
     * 
     * @return type
     * Método listar saidas
     */
    public function listar() {

        $sql = "SELECT*FROM tbl_saidas";

        $stmt = $this->conexao->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);

        return $stmt->fetchAll();        
       
    }
    
    /**
     * 
     * @param Produtos_model $objvo
     * @param type $condicao
     * Método atualizar Saidas 
     */
    public function atualizar($objvo, $condicao) {

        if ($objvo instanceof Saidas_model) {

            try {

                $stmt = $this->conexao->prepare("UPDATE tbl_saidas SET " .
                        "produto=?, funcionario=?, qtdsaida=?". 
                        " WHERE idsaida=?");

                $this->conexao->beginTransaction();

                $stmt->bindValue(1, $objvo->getProduto());
                $stmt->bindValue(2, $objvo->getFuncionario());
                $stmt->bindValue(3, $objvo->getQtdsaida());                
                $stmt->bindValue(4, $condicao);
                $stmt->execute();

                $this->conexao->commit();
                $this->conexao = NULL;
                
            } catch (PDOException $exception) {

                print 'ERROR.::. ' . $exception->getMessage();
            }
            
        } else {

            print 'nao e instancia';
        }
    }
    
    /**
     * 
     * @param type $idsaida
     * @return int
     * Método apagar saida
     */
    public function deletar($idsaida) {

        try {

            $delete = $this->conexao->prepare("DELETE FROM tbl_saidas WHERE " .
                                              "idsaida=$idsaida");
            $delete->execute();

            if ($delete >= '1') {

                return $delete;
                
            } else {

                return 0;
            }
            
        } catch (PDOException $e) {

            print 'ERROR.::. ' . $e->getMessage();
        }
    }
}

